/*
 * @Desc:
 * @author cheegon
 * @date 2024/7/29  下午1:56
**/
import { getToken, removeToken, setToken } from '@/config/authConfig.js'
import { getUserInfo, removeUserInfo, setUserInfo } from '@/utils/storageUtil.js'
import { reqLogin, reqLogout, reqUserInfo } from '@/api/clientApi.js'

const user = {
    // 命名空间,true的话,调用需要添加user/
    namespaced: false,
    state: {
        isLogin: !!getToken(),
        token: getToken(),
        userInfo: getUserInfo(),
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
            state.isLogin = !!token
        },
        SET_INFO: (state, info) => {
            state.userInfo = info
        },
    },
    actions: {
        /**
         * 登录
         * @param commit
         * @param dispatch
         * @param data
         * @returns {Promise<unknown>}
         * @constructor
         */
        login({ commit, dispatch }, data) {
            return new Promise((resolve, reject) => {
                dispatch('getLogin', data).then(() => {
                    dispatch('getUserInfo').then((data) => {
                        resolve(data)
                    }).catch(error => {
                        reject(error)
                    })
                }).catch(res => {
                    reject(res)
                })
            })
        },
        /**
         * 登录
         * @param commit
         * @param dispatch
         * @param data
         * @returns {*}
         * @constructor
         */
        loginWithToken({ commit, dispatch }, data) {
            // const tokenStr = data.tokenHead + data.token
            const tokenStr = data.token
            setToken(tokenStr)
            commit('SET_TOKEN', tokenStr)
            return dispatch('getUserInfo')
        },
        loginWithUserInfo({ commit, dispatch }, data) {
            // const tokenStr = data.tokenHead + data.token
            const tokenStr = data.token
            setToken(tokenStr)
            commit('SET_TOKEN', tokenStr)
            setUserInfo(data.user)
            commit('SET_INFO', data.user)
        },
        /**
         * 登录
         * @param commit
         * @param dispatch
         * @param data
         * @returns {Promise<unknown>}
         * @constructor
         */
        loginNeedMember({ commit, dispatch }, data) {
            return new Promise((resolve, reject) => {
                dispatch('getLogin', data).then(() => {
                    Promise.all([dispatch('getUserInfo'), dispatch('getMemberInfo')])
                           .then(() => {
                               resolve()
                           }).catch(res => {
                        reject(res)
                    })
                }).catch(res => {
                    reject(res)
                })
            })
        },
        /**
         * 获取登录
         * @param commit
         * @param dispatch
         * @param params
         * @returns {Promise<unknown>}
         * @constructor
         */
        getLogin({ commit, dispatch }, params) {
            return new Promise((resolve, reject) => {
                reqLogin(params).then(data => {
                    const tokenStr = data.token
                    setToken(tokenStr)
                    commit('SET_TOKEN', tokenStr)
                    resolve(data)
                }).catch(res => {
                    reject(res)
                })
            })
        },
        /**
         * 获取用户信息
         * @param commit
         * @returns {Promise<unknown>}
         * @constructor
         */
        getUserInfo({ commit }) {
            return new Promise((resolve, reject) => {
                reqUserInfo().then(data => {
                    setUserInfo(data)
                    commit('SET_INFO', data)
                    resolve(data)
                }).catch(res => {
                    reject(res)
                })
            })
        },
        /**
         * 后端登出
         * @param commit
         * @returns {Promise<unknown>}
         * @constructor
         */
        logout({ commit }) {
            return new Promise((resolve) => {
                reqLogout().then(() => {
                    commit('SET_TOKEN', '')
                    removeToken()
                    removeUserInfo()
                    resolve()
                }).catch(() => {
                    commit('SET_TOKEN', '')
                    removeToken()
                    removeUserInfo()
                    resolve()
                })
            })
        },
        /**
         * 前端登出
         * @param commit
         * @returns {Promise<void>}
         * @constructor
         */
        fedLogout({ commit }) {
            commit('SET_TOKEN', '')
            commit('SET_INFO', {})
            removeToken()
            removeUserInfo()
            return Promise.resolve()
        }
    }
}

export default user
